/**
 * @file HTTP异常过滤器
 * @description 统一处理HTTP异常并格式化响应
 * @author GPS轨迹可视化系统
 * @date 2024-02-23
 */

import { ExceptionFilter, Catch, ArgumentsHost, HttpException, HttpStatus } from '@nestjs/common';
import { Response } from 'express';

@Catch()
export class HttpExceptionFilter implements ExceptionFilter {
  /**
   * 捕获并处理异常
   * @param exception 捕获到的异常对象
   * @param host 参数宿主
   * @returns void
   */
  catch(exception: unknown, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse<Response>();
    const status = exception instanceof HttpException ? exception.getStatus() : HttpStatus.INTERNAL_SERVER_ERROR;

    response.status(status).json({
      statusCode: status,
      timestamp: new Date().toISOString(),
      message: exception instanceof HttpException ? exception.getResponse() : 'Internal server error',
    });
  }
} 